package com.action.link;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.link.Link;
import com.business.link.LinkBus;
import com.config.SystemGlobals;
import com.util.MD5;

public class AddLink extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String contentType = SystemGlobals.getValue("contentType");
		PrintWriter out = response.getWriter();
		response.setContentType(contentType);
		String linkName = request.getParameter("linkname");
		String linkUrl = request.getParameter("linkurl");
		String linkDemo = request.getParameter("linkdemo");
		String isShow = request.getParameter("isshow");
		if (isShow == null) {
			isShow = "0";
		}
		String username = request.getParameter("username");
		String action = request.getParameter("action");

		if ("update".equals(action)) {
			LinkBus lb = new LinkBus();

			String linkNo = request.getParameter("linkno");
			Link link = new Link();
			link.setIsShow(isShow);
			link.setLinkDemo(linkDemo);
			link.setLinkName(linkName);
			link.setLinkUrl(linkUrl);
			link.setUsername(username);
			lb.update(link, linkNo);

			RequestDispatcher rd = request
					.getRequestDispatcher("user/linklist.jsp");
			rd.forward(request, response);
		}

		if ("add".equals(action)) {
			MD5 md5 = new MD5();
			String linkNo = md5.getMD5ofStr(linkName + "@" + username);
			Link link = new Link();
			link.setIsShow(isShow);
			link.setLinkDemo(linkDemo);
			link.setLinkName(linkName);
			link.setLinkNo(linkNo);
			link.setLinkUrl(linkUrl);
			link.setUsername(username);
			LinkBus lb = new LinkBus();
			lb.add(link);
			out
					.print("<script>alert(\"添加链接成功\");window.location=\"user/addlink.jsp\";</script>");
			out.close();
		}
	}

}
